package com.taptech.doufu.ui.view.search;

import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.view.View;
import android.widget.LinearLayout;
import com.taptech.doufu.R;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.bean.personalcenter.PersonalCardInfo;
import com.taptech.doufu.ui.adapter.personalcenter.PersonalCardInfoAapater;
import com.taptech.doufu.services.DiaobaoSearchService;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.PullToRefreshListView;
import com.taptech.doufu.ui.view.PullToRefreshListView.OnLoadAndRefreshListener;
import com.taptech.doufu.ui.view.PullToRefreshListView.OnScrollToBottomListener;

/**
 * 用户搜索
 */
public class SearchUserView implements HttpResponseListener,SearchBaseView {
    private Activity activity;
    private View rootView, notContent;
    private View loadView ,nullContentLayout;
    private PullToRefreshListView userListView;
    private int pageIndex;
    private PersonalCardInfoAapater adapter;
    private String userName = "";

    private boolean hasMoreContent = true;
    private boolean isLoadingData = false;

    public SearchUserView(Activity activity, View rootView) {
        this.activity = activity;
        this.rootView = rootView;
        initView();
    }

    private void initView() {
        loadView = rootView.findViewById(R.id.empty_load_root_layout);
        nullContentLayout = rootView.findViewById(R.id.empty_load_layout_null_content);
        userListView = (PullToRefreshListView) rootView.findViewById(R.id.home_search_user_list);
        notContent = (LinearLayout) rootView.findViewById(R.id.home_search_user_not_content);

        userListView.setLoadmoreable(true);
        userListView.setRefreshable(false);
        userListView.setOnLoadAndRefreshListener(new OnLoadAndRefreshListener() {

            @Override
            public void onRefresh() {

            }

            @Override
            public void loadMore() {

            }
        });
        userListView.setOnScrollToBottomListener(new OnScrollToBottomListener() {

            @Override
            public void OnScrollToBottom() {
                loadMoreData();
            }
        });
        adapter = new PersonalCardInfoAapater(activity);
        userListView.setAdapter(adapter);
    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        switch (handleType){
            case DiaobaoSearchService.HANDLE_TYPE_SEARCH_USER:
                loadView.setVisibility(View.GONE);
                try {
                    if (response.getStatus() == 0) {
                        pageIndex = response.getPageIndex();
                        JSONObject datas = (JSONObject) response.getData();
                        JSONArray list = datas.getJSONArray("list");

                        if (pageIndex == 0) {
                            adapter.setDataSource(DiaobaoUtil.jsonArray2BeanList(
                                    PersonalCardInfo.class, list));
                        } else {
                            adapter.setSuperDatas(DiaobaoUtil.jsonArray2BeanList(
                                    PersonalCardInfo.class, list));
                        }
                        hasMoreContent = true;
                        if (list==null || list.length() < Constant.PAGE_SIZE_20) {
                            hasMoreContent = false;
                            userListView.setFull(true);

                        } else {
                            pageIndex = pageIndex + 1;
                        }
                        userListView.loadMoreComplete();
                        //				adapter.notifyDataSetChanged();
                    } else {
                        userListView.loadMoreTimeout();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    isLoadingData = false;
                }
                break;
        }

    }

    @Override
    public void searchContent(String searchContent) {
        if (searchContent.equals(this.userName)) {
            return;
        }
        if (adapter != null) {
            adapter.setSearchContent(searchContent);
            adapter.clearDateSource();
        }
        if (notContent != null) {
            notContent.setVisibility(View.VISIBLE);
        }
        if (adapter != null) {
            adapter.clearDateSource();
        }
        loadView.setVisibility(View.VISIBLE);
        nullContentLayout.setVisibility(View.GONE);

        this.userName = DiaobaoUtil.getStringNoSpace(searchContent);
        pageIndex = 0;
        loadData();
    }
    @Override
    public void cleanData() {
        if (adapter != null) {
            adapter.clearDateSource();
        }
        if (notContent.getVisibility() != View.VISIBLE) {
            notContent.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideFilter(boolean isReset) {

    }

    private synchronized void loadData(){
        if (!isLoadingData) {
            isLoadingData = true;
            DiaobaoSearchService.getInstance().searchHomeUser(userName, pageIndex, this, activity);
        }
    }

    private synchronized void loadMoreData() {
        if (!isLoadingData && hasMoreContent) {
            hasMoreContent = false;
            isLoadingData = true;
            DiaobaoSearchService.getInstance().searchHomeUser(userName, pageIndex, this, activity);
        }
    }

}
